/*
 *Copyright (c) 2008, Scendix Software, Inc.
 * 
 */
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.MediaCenter.UI;
using System.Data.OleDb;
using SKYPE4COMLib;

using Redemption;

namespace mceTapiRexv2
{
    /// <summary>
    /// class representing a contact item
    /// 
    /// </summary>
    public class Contact
    {
        private List<ContactAttribute> properties = new List<ContactAttribute>();
        private String strFirstName;
        private String strLastName;
        private String strCompany;
        private String strTelPrivate;
        private String strTelCompany;
        private String strMobile;
        private String strFax;
        private String strVoip;
        private Image imgPicture;
        /// <summary>
        /// initiates a new contact using information given by a redemption
        /// RDOContactItem object
        /// </summary>
        /// <param name="oContact"></param>
        /// <param name="picture"></param>
        public Contact(RDOContactItem oContact, Image picture)
        {
            if (oContact.FirstName != "" || oContact.LastName != "")
            {
                this.strFirstName = oContact.FirstName;
                this.strLastName = oContact.LastName;
                this.addProperty(1576, oContact.FirstName + " " + oContact.LastName);
            }
            else this.addProperty(1576, oContact.LastNameAndFirstName);
            this.Company = oContact.CompanyName;
            this.addProperty(1496, oContact.CompanyName);

            if (oContact.HomeTelephoneNumber != "")
            {
                this.strTelPrivate = oContact.HomeTelephoneNumber;
                this.addProperty(1500, oContact.HomeTelephoneNumber, "phone");
            }
            else
            {
                this.strTelPrivate = oContact.PrimaryTelephoneNumber;
                this.addProperty(1500, oContact.PrimaryTelephoneNumber, "phone");
            }

            if (oContact.BusinessTelephoneNumber != "")
            {
                this.strTelCompany = oContact.BusinessTelephoneNumber;
                this.addProperty(1625, oContact.BusinessTelephoneNumber, "phone");
            }
            else
            {
                this.strTelCompany = oContact.OtherTelephoneNumber;
                this.addProperty(1625, oContact.OtherTelephoneNumber, "phone");
            }

            this.strMobile = oContact.MobileTelephoneNumber;
            this.addProperty(790, oContact.MobileTelephoneNumber, "phone");

            this.strFax = oContact.TelexNumber;
            this.addProperty(789, oContact.TelexNumber);
            this.strVoip = oContact.NetMeetingAlias;
            this.addProperty(1626, oContact.NetMeetingAlias,"skype");
            if (picture == null && oContact.HasPicture)
                this.imgPicture = mceTapiRexv2.Application.Current.BufferOutlookPicture(oContact);
            else
                this.imgPicture = picture;
        }
        /// <summary>
        /// initiates a new contact object by reading out information stored in 
        /// the database and a given picture
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="picture"></param>
         public Contact(DataRow dr, Image picture)
        {
            this.strFirstName = dr["FirstName"].ToString();
            this.strLastName = dr["LastName"].ToString();
            this.addProperty(1576, dr["FirstName"].ToString() + " " + dr["LastName"].ToString());
            
            this.strCompany = dr["Company"].ToString();
            this.addProperty(1496, dr["Company"].ToString());
            
            this.strTelPrivate = dr["TelPrivate"].ToString();
            this.addProperty(1500, dr["TelPrivate"].ToString(), "phone");
            
            this.strTelCompany = dr["TelCompany"].ToString();
            this.addProperty(1625, dr["TelCompany"].ToString(), "phone");
            
            this.strMobile = dr["Mobile"].ToString();
            this.addProperty(790, dr["Mobile"].ToString(), "phone");
            
            this.strFax = dr["Fax"].ToString();
            this.addProperty(789, dr["Fax"].ToString());
            
            this.strVoip = dr["Voip"].ToString();
            this.addProperty(1627, dr["Voip"].ToString());
            
            this.imgPicture = picture;
            
        }
        /// <summary>
        /// initiates a new contact object by reading out information stored in 
        /// the database
        /// </summary>
        /// <param name="dr"></param>
        public Contact (DataRow dr)
        {
            this.strFirstName = dr["FirstName"].ToString();
            this.strLastName = dr["LastName"].ToString();
            this.addProperty(1576, dr["FirstName"].ToString() + " " + dr["LastName"].ToString());

            this.strCompany = dr["Company"].ToString();
            this.addProperty(1496, dr["Company"].ToString());

            this.strTelPrivate = dr["TelPrivate"].ToString();
            this.addProperty(1500, dr["TelPrivate"].ToString(), "phone");

            this.strTelCompany = dr["TelCompany"].ToString();
            this.addProperty(1625, dr["TelCompany"].ToString(), "phone");

            this.strMobile = dr["Mobile"].ToString();
            this.addProperty(790, dr["Mobile"].ToString(), "phone");

            this.strFax = dr["Fax"].ToString();
            this.addProperty(789, dr["Fax"].ToString());

            this.strVoip = dr["Voip"].ToString();
            this.addProperty(1627, dr["Voip"].ToString());
            if (dr["hasPicture"].ToString().Equals("-1"))
                this.imgPicture = mceTapiRexv2.Application.Current.getPicture(dr["ContactGUID"].ToString());
        }
        /// <summary>
        /// initiates a new contact object using a SKYPE.User object
        /// 
        /// </summary>
        /// <param name="sUser"></param>
        public Contact(SKYPE4COMLib.User sUser)
        {
            this.strLastName = sUser.FullName;
            this.addProperty(1576, sUser.FullName);
        
            this.strCompany = sUser.City + "(" + sUser.Country + ")";
            this.addProperty(1628, sUser.City + "(" + sUser.Country + ")");
      
            this.strTelPrivate = sUser.PhoneHome;
            this.addProperty(1500, sUser.PhoneHome);

            this.strTelCompany = sUser.PhoneOffice;
            this.addProperty(1625, sUser.PhoneOffice);
            
            this.strMobile = sUser.PhoneMobile;
            this.addProperty(790, sUser.PhoneMobile);
            
            this.strVoip = sUser.Handle;
            this.addProperty(839, sUser.Handle, "skype");
            
            string imgPath = mceTapiRexv2.Application.Current.GetPictureFromSkype(sUser.Handle);
            if (imgPath != "")
            {
                this.imgPicture = new Microsoft.MediaCenter.UI.Image("file://" + imgPath);
           }
        
        }

        private void addProperty(int resId, string value, string callBy)
        {
            if (value != null && value != "")
            {
                this.properties.Add(new ContactAttribute(mceTapiRexv2.Application.Current.getResString("" + resId), value.Trim(), callBy));
            }

        }


        private void addProperty(int resId, string value)
        {
            if (value != null && value !="")
            {
                this.properties.Add(new ContactAttribute(mceTapiRexv2.Application.Current.getResString("" + resId), value.Trim()));
            }
           
        }
        public List<ContactAttribute> Properties
        {
            get { return properties; }
            set { properties = value; }
        }
        /// <summary>
        /// gets first 2 Elements that dont equal name or company
        /// </summary>
        public List<ContactAttribute> shortProperties
        {
            get
            {
                int nElements = 0;
                List<ContactAttribute> returnList = new List<ContactAttribute>();
                foreach (ContactAttribute attr in properties)
                {
                    if (attr.Key != mceTapiRexv2.Application.Current.getResString("" + 1576) &&
                        attr.Key != mceTapiRexv2.Application.Current.getResString("" + 1496))
                    {
                        returnList.Add(attr);
                        nElements++;
                        if (nElements >= 2) break; ;
                    }
                }
                return returnList;

            }
            set { }
        }
        /// <summary>
        /// checks wether a given ContactAttribute is a number that can be called
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        private static bool bIsNumber(ContactAttribute attr)
        {
            return attr.isNumber();
        }
        /// <summary>
        /// gets recent calls with contact and navigates to call list
        /// </summary>
        public void getCallsWithContact()
        {
            List<ContactAttribute> numbers = this.properties.FindAll(bIsNumber);
            string cond;
            string callerid =" ( "; string calledid=" ( ";
            int i =0;
            foreach (ContactAttribute number in numbers)
            {
                if (i++ > 0)
                {
                    callerid +=" OR ";
                    calledid +=" OR ";
                }
                callerid += "CallerID LIKE '%" + number.Value +"%'";
                calledid += "CalledID LIKE '%" + number.Value +"%'";

            }
            calledid +=" ) "; callerid += " ) ";
            string callname = " ( CallerName LIKE '%" + Name + "%' OR CalledName LIKE '%" +
                Name + "%')";
            cond = " WHERE " + callerid + " OR " + calledid + " OR " + callname;
            Phonecalls calls = new Phonecalls();
            calls.Condition = cond;
            calls.Subtitle = FirstName;
            mceTapiRexv2.Application.Current.GoToPage("Calls", "", "Phonecalls", calls);

        }
        public String callsWith
        {
            get
            {
                if (this.FirstName != "")
                    return mceTapiRexv2.Application.Current.getResString("1633", this.FirstName);
                else
                    return mceTapiRexv2.Application.Current.getResString("1497");
            }
        }
        public String Name
        {
            get {
                if ((strFirstName!=null && strFirstName != "") && (strLastName!=null && strLastName != ""))
                    return strLastName + ", " + strFirstName;
                else
                    return (strFirstName != "") ? strFirstName : strLastName;
                }
            set { strFirstName = value; }
        }
        public String FirstName
        {
            get { return strFirstName; }
            set { strFirstName = value; }
        }
        public String LastName
        {
            get { return strLastName; }
            set { strLastName = value; }
        }
        public String Company
        {
            get { return strCompany; }
            set { strCompany = value; }
        }
        public String TelPrivate
        {
            get { return strTelPrivate; }
            set { strTelPrivate = value; }
        }
        public String TelCompany
        {
            get { return strTelCompany; }
            set { strTelCompany = value; }
        }
        public String Mobile
        {
            get { return strMobile; }
            set { strMobile = value; }
        }
        public String Fax
        {
            get { return strFax; }
            set { strFax = value; }
        }
        public String Voip
        {
            get { return strVoip; }
            set { strVoip = value; }
        }
        public Image Picture
        {
            get { return imgPicture; }
            set { imgPicture = value; }
        }
    }
}
